package cn.android.mydata2;


import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.googlecode.droidwall.MainActivity;

import java.util.ArrayList;
import java.util.List;

import cn.android.mydata2.dao.TrafficDao;
import cn.android.mydata2.entities.TrafficModel;
import cn.android.mydata2.service.HorizonService;
import cn.android.mydata2.utils.DividerItemDecoration;
import cn.android.mydata2.utils.TrafficUtils;


public class MyMainActivity extends AppCompatActivity {


    private RecyclerView mRecyclerView;
    private FloatingActionButton mFAB;

    TrafficDao dao = new TrafficDao();
    List<TrafficModel> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mFAB = (FloatingActionButton) findViewById(R.id.fab);
        mFAB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyMainActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
        mRecyclerView = (RecyclerView) findViewById(R.id.rvToDoList);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mRecyclerView.addItemDecoration(new DividerItemDecoration(MyMainActivity.this, DividerItemDecoration.VERTICAL_LIST));
        initData();
        mRecyclerView.setAdapter(new MyAdapter(data));


//        Intent intent = new Intent(this, HorizonService.class);
//        startService(intent);
//        TrafficUtils.fetchOneAppData(10063,this);
    }

    private void initData() {
        PackageManager pm = getPackageManager();
        List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
        for (ApplicationInfo packageInfo : packages) {
            //Log.e("TAG","UID = "+packageInfo.uid);
            if (packageInfo.packageName.contains("com.android") || packageInfo.loadLabel(getPackageManager()).toString().contains("com")) {
                continue;
            }
            TrafficModel model = dao.getById(packageInfo.uid);
            if (model == null) {
                continue;
            }
            model.setAppName(packageInfo.loadLabel(getPackageManager()).toString());
            model.setIcon(packageInfo.loadIcon(pm));

            data.add(model);
            //Log.e("TAG","App name:"+packageInfo.loadLabel(getPackageManager()).toString());
        }
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        public List<TrafficModel> mdata;

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler, parent, false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        public MyAdapter(List<TrafficModel> mdata) {
            //super();
            this.mdata = mdata;

        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.tv1.setText(String.valueOf(mdata.get(position).getAppName()));
            holder.tv2.setText("使用流量：" + dao.dataSizeFormat(mdata.get(position).getCell()));
//            holder.tv2.setText(String.valueOf(mdata.get(position).getCell()/1024/1024));
            holder.iv.setImageDrawable(mdata.get(position).getIcon());
        }

        @Override
        public int getItemCount() {
            return mdata.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            public ImageView iv;
            public TextView tv1;
            public TextView tv2;

            public ViewHolder(View itemView) {
                super(itemView);
                iv = (ImageView) itemView.findViewById(R.id.imageView);
                tv1 = (TextView) itemView.findViewById(R.id.textView1);
                tv2 = (TextView) itemView.findViewById(R.id.textView2);
            }
        }
    }
}
